<?php
require_once "xcx/wxBizDataCrypt.php";
class Wxxcx{
    private $wx;
    public function __construct($wx)
    {
        $this->wx;
    }

    /**
     * 获取小程序的session和openid
     * @param $code
     * @return array
     */
    public function getXcxUserInfo($code)
    {
        // 小程序专用信息
        $url = "https://api.weixin.qq.com/sns/jscode2session?appid={$this->wx['xcx']['appid']}&secret={$this->wx['xcx']['secret']}&js_code={$code}&grant_type=authorization_code";
        $res = $this->http_request($url);
        return $res;
    }

    /**
     * 小程序解密
     */
    public function userinfodecode($sessionKey,$encryptedData,$iv){
        $pc = new WXBizDataCrypt($this->wx['xcx']['appid'], $sessionKey);
        $errCode = $pc->decryptData($encryptedData, $iv, $data );
        if ($errCode == 0) {
            return $data;
        } else {
            //print($errCode);
            return [];
        }
    }

    /**
     * 获取小程序的accesstoken
     */
    public function getaccesstoken(){
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->wx['xcx']['appid']}&secret={$this->wx['xcx']['secret']}";
        $result = $this->http_request($url);
        if(isset($result['errcode'])){
            return ['code'=>false,'msg'=>$result['errmsg']];
        }else{
            return ['code'=>true,'msg'=>'请求成功','data'=>$result['access_token']];
        }
    }

    /**
     * 获取小程序码
     */
    public function getcode($h_params,$accesstoken){
        $url="https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={$accesstoken}";
        $result = $this->httpRequest($url,json_encode($h_params),'POST');
        if($this->isJson($result)){
            return ['code'=>false,'msg'=>$this->isJson($result)->errmsg];
        }else{
            return ['code'=>true,'msg'=>$result];
        }
    }

    /**
     * 判断字符串是否是json字串
     */
    function isJson($data = '', $assoc = false){
        $data = json_decode($data, $assoc);
        if (($data && is_object($data)) || (is_array($data) && !empty($data))) {
            return $data;
        }
        return false;
    }



    //把请求发送到微信服务器换取二维码
    function httpRequest($url, $data='', $method='GET'){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
        if($method=='POST')
        {
            curl_setopt($curl, CURLOPT_POST, 1);
            if ($data != '')
            {
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            }
        }
        curl_setopt($curl, CURLOPT_TIMEOUT, 30);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $result = curl_exec($curl);
        curl_close($curl);
        return $result;
    }


    /**
     * curl模拟http请求GET/POST
     * @param $url
     * @param null $data
     * @return array
     */
    public function http_request($url, $data = null)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        // 以文件流形式返回
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        if (!empty($data))
        {
            // POST请求
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        $output = curl_exec($ch);
        curl_close($ch);
        // 返回数组
        return json_decode($output, true);
    }

}
